//
// Created by mazaiting on 2024/4/28.
//

const int MaxSize = 100;        //队列的容量
template<typename T>
class CSqQueue1            //循环队队列类模板
{
public:
    T *data;                //存放队中元素
    int front;                //队头指针
    int count;                //队中元素个数
    CSqQueue1()                //构造函数
    {
        data = new T[MaxSize];        //为data分配容量为MaxSize的空间
        front = 0;                //队头指针置初值
        count = 0;                //元素个数置初值
    }

    ~CSqQueue1()            //析构函数
    {
        delete[] data;
    }

    //----------循环队基本运算算法-----------------
    bool empty()                //判队空运算
    {
        return count == 0;
    }

    bool push(T e)                //进队列运算
    {
        if (count == MaxSize)        //队满上溢出
            return false;
        int rear1 = (front + count) % MaxSize;    //求队尾（rear1为局部变量）
        rear1 = (rear1 + 1) % MaxSize;        //队尾循环进1
        data[rear1] = e;
        count++;                //元素个数增1
        return true;
    }

    bool pop(T &e)            //出队列运算
    {
        if (count == 0)            //队空下溢出
            return false;
        front = (front + 1) % MaxSize;
        e = data[front];
        count--;                //元素个数减少1
        return true;
    }

    bool gethead(T &e)            //取队头运算
    {
        if (count == 0)            //队空下溢出
            return false;
        int head = (front + 1) % MaxSize;
        e = data[head];
        return true;
    }
};

